package com.basic.common.exception.handle;

import com.basic.common.business.BasicDsUtil;
import com.basic.common.exception.Errors;
import com.basic.common.exception.SystemCodeDsBean;
import com.basic.common.exception.business.*;
import com.basic.common.json.JsonDsUtil;
import com.basic.common.result.WebResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.web.HttpMediaTypeNotSupportedException;

import javax.servlet.http.HttpServletRequest;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author
 * @explain
 * @since
 */
public class ExceptionDsUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionDsUtil.class);

    public static WebResult getErrorByException(Exception e) {
        String code = "";
        String exception = "";
        String msg = "";
        if (e instanceof NullPointerException) {
            code = "1101";
            exception = "NullPointerException";
            msg = "空指针异常";
        } else if (e instanceof ArithmeticException) {
            code = "1102";
            exception = "ArithmeticException";
            msg = "算术异常";
        } else if (e instanceof NumberFormatException) {
            code = "1103";
            exception = "NumberFormatException";
            msg = "字符串转换为数字异常";
        } else if (e instanceof ClassCastException) {
            code = "1104";
            exception = "ClassCastException";
            msg = "类型强制转换异常";
        } else if (e instanceof NegativeArraySizeException) {
            code = "1105";
            exception = "NegativeArraySizeException";
            msg = " 创建一个大小为负数的数组错误异常";
        } else if (e instanceof ArrayIndexOutOfBoundsException) {
            code = "1106";
            exception = "ArrayIndexOutOfBoundsException";
            msg = "数组下标越界异常";
        } else if (e instanceof FileNotFoundException) {
            code = "1107";
            exception = "FileNotFoundException";
            msg = "文件未找到异常";
        } else if (e instanceof SQLException) {
            code = "1108";
            exception = "SQLException";
            msg = "操作数据库异常";
        } else if (e instanceof EOFException) {
            code = "1114";
            exception = "EOFException";
            msg = "文件已结束异常";
        } else if (e instanceof IOException) {
            code = "1109";
            exception = "IOException";
            msg = "输入输出异常";
        } else if (e instanceof NoSuchMethodException) {
            code = "1110";
            exception = "NoSuchMethodException";
            msg = "方法未找到异常";
        } else if (e instanceof IllegalArgumentException) {
            code = "1111";
            exception = "IllegalArgumentException";
            msg = "传递非法参数异常";
        } else if (e instanceof IllegalAccessException) {
            code = "1112";
            exception = "IllegalAccessException";
            msg = "没有访问权限";
        } else if (e instanceof HttpMediaTypeNotSupportedException) {
            code = "1113";
            exception = "HttpMediaTypeNotSupportedException";
            msg = "访问参数类型异常";
        } else if (e instanceof DataAccessException) {
            code = "1116";
            exception = "DataAccessException";
            msg = "数据库异常";
        } else {
            code = "1199";
            exception = e.getMessage();
            msg = "系统异常";
        }
        return WebResult.fail(code, exception, msg, null);
    }

    public static WebResult getExceptionMsg(HttpServletRequest req, Exception e) {
        String errorStack = null;// Throwables.getStackTraceAsString(e);
        WebResult webResult;
        String type = "ERROR";
        String code;
        String msg;
        String exception1;
        if (e instanceof ValidationException) {
            LOGGER.warn("ValidationException Request: {}", req.getRequestURI());
            ValidationException exception = (ValidationException) e;
            code = exception.getCode();
            msg = exception.getMsg();
            exception1 = exception.getException();
        } else if (e instanceof LoginException) {
            LOGGER.warn("LoginException Request: {}", req.getRequestURI());
            LoginException exception = (LoginException) e;
            code = exception.getCode();
            msg = exception.getMsg();
            exception1 = exception.getException();
        } else if (e instanceof ServerServiceException) {
            LOGGER.error("ServerServiceException Request: {}", req.getRequestURI());
            LOGGER.error("ServerServiceException ErrorStack: {}", errorStack);
            ServerServiceException exception = (ServerServiceException) e;
            code = exception.getCode();
            exception1 = exception.getException();
            msg = exception.getMsg();
        } else if (e instanceof BusinessException) {
            LOGGER.info("BusinessException Request: {}", req.getRequestURI());
            LOGGER.info("BusinessException ErrorStack: {}", errorStack);
            BusinessException exception = (BusinessException) e;
            code = exception.getCode();
            msg = exception.getMsg();
            exception1 = exception.getException();
            Object object = exception.getObject();
            //插入异常信息
            if (object != null) {
                if (object instanceof ExceptionReqDsBean) {
                    ExceptionReqDsBean exceptionReqForm = (ExceptionReqDsBean) object;
                    exceptionReqForm.setMsg(msg);
                    BasicDsUtil.add(exceptionReqForm);
                }
            }
        } else if (e instanceof ExternalSystemException) {
            LOGGER.error("ExternalSystemException Request: {}", req.getRequestURI());
            LOGGER.error("ExternalSystemException ErrorStack: {}", errorStack);
            ExternalSystemException exception = (ExternalSystemException) e;
            type = exception.getType();
            code = exception.getCode();
            msg = exception.getMsg();
            exception1 = exception.getException();
        } else {
            LOGGER.error("OtherException Request: {}", req.getRequestURI());
            LOGGER.error("OtherException ErrorStack: {}", errorStack);
            WebResult errorWebResult = getErrorByException(e);
            code = errorWebResult.getCode();
            exception1 = errorWebResult.getException();
            msg = Errors.SYSTEM_BUSINESS_ERROR.msg;
        }
        webResult = getMsg(type, code, exception1, msg);
        LOGGER.warn("Exception Result: {}", JsonDsUtil.beanToJSONStr(webResult));
        return webResult;
    }

    public static WebResult getMsg(String type, String code, String exception, String msg) {
        boolean b = StringUtils.isBlank(msg) || StringUtils.isBlank(exception);
        if (b) {
            Errors errors = Errors.getErrors(code);
            if (StringUtils.equalsIgnoreCase(type, "ERROR") && errors != null) {
                exception = errors.exception;
                msg = Errors.SYSTEM_BUSINESS_ERROR.msg;
            } else {
                SystemCodeDsBean systemCodeDsBean = BasicDsUtil.selErrorByTypeAndCode(type, code);
                if (systemCodeDsBean != null) {
                    exception = systemCodeDsBean.getValue();
                    msg = systemCodeDsBean.getMsg();
                }
            }
        }
        return WebResult.fail(code, exception, msg, null);
    }
}
